<?php

namespace Liaiyong\Light\Core;

use Liaiyong\Light\Base\Actionizable;
use Liaiyong\Light\Base\Singleton;
use Liaiyong\Light\Core\App;
use Liaiyong\Light\Core\EventEmitter;
use Liaiyong\Light\Core\Template;
use Liaiyong\Light\Util\Logger;
use Liaiyong\Light\Util\Utility;

abstract class Action extends Singleton implements Actionizable {
    // use Singleton;
    /**
     * 事件常量，创建时
     *
     * @var string
     */
    const E_CREATE = 'action:event:create';
    /**
     * 事件常量，验证时
     */
    const E_VALIDATE = 'action:event:validate';
    /**
     * 事件常量，LANUCH
     *
     * @var string
     */
    const E_LANUCH = 'action:event:lanuch';
    /**
     * 事件常量，渲染时
     *
     * @var string
     */
    const E_RENDER = 'action:event:render';
    
    /**
     * App
     *
     * @var App
     */
    protected $app;
    /**
     * EventEmitter
     *
     * @var EventEmitter
     */
    protected $event;
    /**
     * 模板引擎对象
     *
     * @var Template
     */
    protected $template;
    
    /**
     * 构造方法
     *
     * @return Action
     */
    protected function __construct() {
    }
    /**
     * Action初始化
     *
     * @return void
     */
    public function initialize() {
        $this->app = App::$_app;
        $this->event = App::$_event;
        $this->template = Template::getInstance();
        // listen
        $this->event->listen(get_class($this), Action::E_VALIDATE, array($this, 'afterValidate'));
    }
    /**
     * Action生命同期
     *
     * @return void
     */
    public function lifecycle() {
        $class= get_class($this);
        // on create
        $this->onCreate();
        $this->event->fire($class, Action::E_CREATE, array (
                $this 
        ));
        $valid = $this->onValidate();
        $this->event->fire($class, Action::E_VALIDATE, array (
                $this, $valid
        ));
        if(!empty($valid)) {
            $this->onLanuch();
            $this->event->fire($class, Action::E_LANUCH, array (
                    $this
            ));
        }
        // render
        $this->onRender();
        $this->event->fire($class, Action::E_RENDER, array (
                $this 
        ));
    }

    /**
     * 监听验证事件,onLanuch等前执行
     * @return void
     */
    public function afterValidate($me, $valid) {
        if(!empty($valid)) {
            $this->validated();
        } else {
            $this->invalidated();
        }
    }
    /**
     * 验证成功时,onLanuch等前执行
     * @return void
     */
    protected function validated() {

    }
    /**
     * 验证不成功时,onLanuch等前执行
     * @return void
     */
    protected function invalidated() {
        
    }
    /**
     * 返回App
     *
     * @return App
     */
    public function getApp() {
        return $this->app;
    }
    /**
     * 返回Api名称
     *
     * @return string
     */
    public function getApiname() {
        return $this->app->getApiname();
    }
    /**
     * 返回模板引擎对象
     *
     * @return Template
     */
    public function getTemplate() {
        return $this->template;
    }
    /**
     * 将数据PUSH到Template中
     *
     * @param string|array|object $name
     * @param mixed $value   
     * @return void
     */
    protected function push($name, $value = null) {
        $this->template->push($name, $value);
    }
    /**
     * 设置Template使用的模板文件中
     *
     * @param string $filename
     * @return void
     */
    protected function file($filename) {
        $this->template->file($filename);
    }
    
    /**
     * Action实例化后执行方法
     */
    public function onCreate() {
    }
    /**
     * 执行onLanuch方法前的验证方法;如果返回true，执行onLanuch方法.
     * 默认返回true
     * @return boolean
     */
    public function onValidate() {
        return true;
    }
    /**
     * 执行事件
     */
    public function onLanuch() {
    }
    /**
     * 渲染输出内容
     */
    public function onRender() {
        $this->template->display();
    }
}
// PHP END
